/*
 * @Author: huangkaicheng@rfchina.com
 * @Date: 2020-09-08 16:54:02
 * @LastEditTime: 2020-09-08 16:56:53
 * @LastEditors: Please set LastEditors
 * @Description: Compress Image
 * @FilePath: \rf-auto-jenkins-plugin\lib\imgMin.js
 */
const inquirer = require('inquirer')
const ImgMin = require('../imgMin/imgMin')
const {
  resolve,
  log,
  successLog,
  errorLog,
  showLoading,
  hideLoading,
} = require('../utils.js')
module.exports = function imgMin(program) {
  program
    .command('imgMin')
    // .option('-k, --key [key]', `Tinypng's key, Required`) //暂时先写死一个apikey,后面扩展多几个，因为一个apikey一个月最多压缩500张
    .option('-p, --path [path]', `Compress directory`)
    .description('Compress images by tinypng api')
    .action(async (options) => {
      const conf = {}
      if (options.path) {
        console.log(options.path)
        var reg = /^["|'](.*)["|']$/g
        conf.imgMinPath = options.path.replace(reg, '$1') //把路径前后的单引号或者双引号去掉
        const imgMin = new ImgMin(conf)
        imgMin.start()
        return
      } else {
        inquirer
          .prompt([
            {
              type: 'input',
              name: 'root',
              message:
                '请输入压缩路径(如./images,代表根目录下的images目录下的所有图片都压缩，不填则默认从根目录往下递归):',
            },
          ])
          .then(async function (res) {
            let { root } = res
            console.log('输入的路径', root)
            conf.imgMinPath = root
            showLoading({ message: '执行中···' })
            const imgMin = new ImgMin(conf)
            hideLoading()
            await imgMin.start()
          })
      }
    })
}
